//! # RustCloud 可观测性框架
//!
//! 提供企业级的可观测性功能，包括监控、健康检查、指标收集和告警系统。
//! 
//! ## 核心功能
//! 
//! - **监控系统**：实时监控各种系统指标和业务指标
//! - **健康检查**：支持HTTP、TCP、自定义健康检查
//! - **指标收集**：收集、聚合和导出各种指标数据
//! - **告警系统**：基于阈值的智能告警和通知
//! 
//! ## 快速开始
//! 
//! ```rust
//! use rustcloud_observability::prelude::*;
//! 
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     // 创建监控系统
//!     let monitor = DefaultMonitor::new(MonitorConfig::default());
//!     
//!     // 创建健康检查器
//!     let health_checker = DefaultHealthChecker::new();
//!     
//!     // 创建指标收集器
//!     let metrics_collector = DefaultMetricsCollector::new();
//!     
//!     Ok(())
//! }
//! ```

pub mod monitoring;
pub mod health;
pub mod metrics;
pub mod alerting;

// 重新导出核心类型和 trait
pub use monitoring::*;
pub use health::*;
pub use metrics::*;
pub use alerting::*;

/// 便利的 prelude 模块，包含常用的类型和 trait
pub mod prelude {
    // 监控相关类型
    pub use crate::monitoring::{
        Monitor, MonitoringConfig, MonitoringMetrics, ThroughputMetrics,
        LatencyMetrics, ErrorRateMetrics, ResourceUsageMetrics,
        InMemoryMonitor, PerformanceMonitor, PerformanceStats,
        SystemHealthChecker, MonitoringError
    };
    
    // 健康检查相关类型 - 只导出存在的类型
    pub use crate::health::{
        HealthChecker, HealthCheckResult, HealthStatus, ComponentHealth,
        HealthCheckManager, DatabaseHealthChecker, HttpHealthChecker, 
        MemoryHealthChecker, HealthCheckConfig
    };
    
    // 指标收集相关类型 - 只导出存在的类型
    pub use crate::metrics::{
        MetricRegistry, Counter, Gauge, Histogram, Summary,
        HistogramValue, SummaryValue, MetricCollector,
        SystemMetricsCollector, MetricType, MetricValue,
        MetricMetadata, MetricSample, Labels
    };
    
    // 告警相关类型 - 只导出存在的类型
    pub use crate::alerting::{
        AlertManager, AlertRule, AlertHandler, Alert, AlertLevel
    };
    
    // 通用错误和结果类型
    pub use crate::{ObservabilityError, ObservabilityResult};
}

/// 重新导出核心错误类型
pub use rustcloud_core::{ServiceError, ServiceResult};

/// 可观测性结果类型
pub type ObservabilityResult<T> = Result<T, ObservabilityError>;

/// 可观测性错误类型
#[derive(Debug, Clone, thiserror::Error)]
pub enum ObservabilityError {
    #[error("监控错误: {0}")]
    MonitorError(String),
    
    #[error("健康检查错误: {0}")]
    HealthCheckError(String),
    
    #[error("指标收集错误: {0}")]
    MetricsError(String),
    
    #[error("告警错误: {0}")]
    AlertError(String),
    
    #[error("配置错误: {0}")]
    ConfigError(String),
    
    #[error("网络错误: {0}")]
    NetworkError(String),
    
    #[error("内部错误: {0}")]
    InternalError(String),
}

impl ObservabilityError {
    /// 创建监控错误
    pub fn monitor_error(msg: impl Into<String>) -> Self {
        Self::MonitorError(msg.into())
    }
    
    /// 创建健康检查错误
    pub fn health_check_error(msg: impl Into<String>) -> Self {
        Self::HealthCheckError(msg.into())
    }
    
    /// 创建指标错误
    pub fn metrics_error(msg: impl Into<String>) -> Self {
        Self::MetricsError(msg.into())
    }
    
    /// 创建告警错误
    pub fn alert_error(msg: impl Into<String>) -> Self {
        Self::AlertError(msg.into())
    }
    
    /// 创建配置错误
    pub fn config_error(msg: impl Into<String>) -> Self {
        Self::ConfigError(msg.into())
    }
    
    /// 创建网络错误
    pub fn network_error(msg: impl Into<String>) -> Self {
        Self::NetworkError(msg.into())
    }
    
    /// 创建内部错误
    pub fn internal_error(msg: impl Into<String>) -> Self {
        Self::InternalError(msg.into())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_observability_error_creation() {
        let error = ObservabilityError::monitor_error("test error");
        assert!(matches!(error, ObservabilityError::MonitorError(_)));
        
        let error = ObservabilityError::health_check_error("health check failed");
        assert!(matches!(error, ObservabilityError::HealthCheckError(_)));
        
        let error = ObservabilityError::metrics_error("metrics collection failed");
        assert!(matches!(error, ObservabilityError::MetricsError(_)));
        
        let error = ObservabilityError::alert_error("alert processing failed");
        assert!(matches!(error, ObservabilityError::AlertError(_)));
    }
}